package com.rabbitmq.web;

import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: TongRui乀
 * @Date: 2019/8/2 10:33
 * @description：
 */
@RestController
@RequestMapping("web")
public class WebController {

    @Autowired
    private Environment env;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("env/{key}")
    public String getEnv(@PathVariable String key){
        return env.getProperty(key);
    }


    @GetMapping("send/{message}")
    public String sendRabbit(@PathVariable String message){

        this.rabbitTemplate.setExchange(env.getProperty("log.exchange.name"));
        this.rabbitTemplate.setRoutingKey(env.getProperty("log.routing.key.name"));

        this.rabbitTemplate.convertAndSend(MessageBuilder.withBody(message.getBytes()).build());

        return "SUCCESS";
    }

    @GetMapping("order")
    public String killOrder(){

        this.rabbitTemplate.setExchange(env.getProperty("order.exchange.name"));
        this.rabbitTemplate.setRoutingKey(env.getProperty("order.routing.key.name"));

        this.rabbitTemplate.convertAndSend(MessageBuilder.withBody("抢单".getBytes()).build());

        return "SUCCESS";
    }

}
